//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"

@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 程序的组成结构
    //1.main.m 主要实现了程序的正常运行
    //2. APPdelegate 程序的执行者
    //签订了UIaplicationdelegate(协议方法可选)
    //注意:APPdelegate就是一个普通的类 只不过签订协议 才能得到所有方法
    //3.ViewController 视图控制器 主要负责视图管理 看得见 摸得到
    //4.Main.sb(视图管理) LaunchScreen.sb(负责启动页) 可视化管理 (xcode7 以前 LaunchScreen.xib)
    //5.Assets.xcassets 主要用来管理图片素材(Images.xcassets)
    //6.InFo.plist (工程配置文件)
    
    //模拟器
    //command + 1~5 模拟器大小
    //command + ← / → 改变模拟器方向 横竖屏切换
    
    //iOS概述
    //注意大小写iOS
    
    //1.UIWindow(程序中 只有一个主window)
    // 1> 取消程序的可视化入口, Main.sb
    // 2> 创建主window对象 使其大小与屏幕一样
    //UIScreen 系统屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //2.屏幕背景颜色
    self.window.backgroundColor = [UIColor whiteColor];
    //3.让window成为主窗口 并显示
    [_window makeKeyAndVisible];
    //4.设置window的主窗口
    //注意xcode7以后 必须设置rootViewController 否则 在该方法结束会Crash
    _window.rootViewController = [[ViewController alloc] init];
    //3> UIwindow 继承与UIView
    //2.UIView(与NSOBject相似 是UI阶段的根类)
    //学习新类 //
    // 1> 需要新类包含哪些初始化方法
    // 2>包含哪些行为(方法)和特征(属性)
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
//    view.backgroundColor = [UIColor greenColor];
//    [_window addSubview:view];
    
    //坐标系系统
    //1.与数学中的坐标系不同 y的正方向向下
    //2.原点是屏幕的左上角
    
    //frame center bounds
    //1.frame(x, y, width, height) 视图在其俯视图上的位置
//    view.frame = CGRectMake(10, 10, 80, 80);
    //2.bounds(x, y, width, height) 视图本身的坐标系(0, 0, width, height)
    //bounds 的x/y影响子视图的frame.改变了坐标系
    //bounds width height 修改的是本省的大小 会以中心店收缩
    // bounds x, y 修改的本身的坐标系 影响它的子视图
    //bounds的改变不会改变该视图的位置
//    NSLog(@"改变前%@", NSStringFromCGRect(view.frame));
//    NSLog(@"改变前的center%@", NSStringFromCGPoint(view.center));
//    view.bounds = CGRectMake(10, 10, 200, 200);
//    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
//    view1.backgroundColor = [UIColor redColor];
//    [view addSubview:view1];
//    NSLog(@"改变后%@", NSStringFromCGRect(view.frame));
//    NSLog(@"改变后的center%@", NSStringFromCGPoint(view.center));

    //3.center 中心点
    //    view.center = _window.center;
    //    view1.center = CGPointMake(120, 120);
    
    //改变绿色view的大小 变成100
    // view.frame get方法
    // frame.size 结构体
    // OC和C内容结合 是一个常量i
//    view.frame = CGRectMake(0, 0, 100, 160);
//    CGRect rect = view.frame;
//    rect.size.width = 100;
//    view.frame = rect;
//    
//    view.x_width = 100;
    
    
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [_window addSubview:testView];
    //1.背景颜色
    testView.backgroundColor = [UIColor cyanColor];
    //2.alpha 透明度0~1
    testView.alpha = 0.2;
    //3.tag 标记(编号)
    testView.tag = 100;
    //根据tag找到对应view __kindOf
    UIView *tagView = [_window viewWithTag:100];
    //4.subviews
    
    UIView *arrayOfView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayOfView);
    
    //5.superViews
//    testView.superview
    
    //6.hidden 隐藏/显示
    testView.hidden = YES;
    
    //视图层级
    //addSubview
    //testView insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>
    //testView insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>
    //testView insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    [_window addSubview:view1];
    view1.backgroundColor = [UIColor greenColor];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
    [_window insertSubview:view2 atIndex:0];
    view2.backgroundColor = [UIColor redColor];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 50, 50)];
    [_window insertSubview:view3 aboveSubview:view2];
    view3.backgroundColor = [UIColor purpleColor];

    //label(用来显示文本)
    //如果类没有自己的初始化方法 则使用父类的
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 80, 180, 40)];
    [_window addSubview:label];
    label.backgroundColor = [UIColor cyanColor];
    
    //label的属性
    label.text = @"I am a girl, go to shopping.I am a girl, go to shopping.";
    label.textColor = [UIColor blueColor];
    label.font = [UIFont systemFontOfSize:14];
    label.font = [UIFont boldSystemFontOfSize:12];//加粗
    label.textAlignment = NSTextAlignmentCenter;//默认向左对齐
        label.font = [UIFont fontWithName:@"Helvetica" size:10];
    label.numberOfLines = 3;//不确定行数时 给0
    //阴影
    label.shadowColor = [UIColor yellowColor];
    //阴影大小(负往左 正往右)
    label.shadowOffset = CGSizeMake(-2, 1);
//    label.lineBreakMode = NSLineBreakByWordWrapping;;//按单词换行
    label.lineBreakMode = NSLineBreakByClipping;
    //换行模式 位置
    
    //UIbutton
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//系统的按钮类型
    button.backgroundColor = [UIColor yellowColor];
    [_window addSubview:button];
    button.frame = CGRectMake(120, 240, 50, 30);
    //添加事件
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    //给button添加文字
    [button setTitle:@"YES" forState:UIControlStateNormal];
    //修改button的文字颜色
    [button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];//系统的按钮类型
    button1.backgroundColor = [UIColor yellowColor];
    [_window addSubview:button1];
    button1.frame = CGRectMake(200, 240, 50, 30);
    //添加事件
    [button1 addTarget:self action:@selector(handleButton1:) forControlEvents:UIControlEventTouchUpInside];
    //给button添加文字
    [button1 setTitle:@"DIE" forState:UIControlStateNormal];
    //修改button的文字颜色
    [button1 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    
    //textfield
    UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(100, 130, 180, 40)];
    [_window addSubview:text];
    text.backgroundColor = [UIColor orangeColor];
    //给输入框赋初值
//    text.text = @"are you ready?";
    text.textAlignment = 1;
    text.textColor = [UIColor blueColor];
    text.borderStyle = 1;//UITextBorderStyleNone默认无 UITextBorderStyleRoundedRect圆角 UITextBorderStyleLine边缘线 UITextBorderStyleBezel
    text.placeholder = @"你想做英雄吗";
    text.clearsOnBeginEditing = YES;
    //command + k 显示或收起键盘
    UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 180, 180, 40)];
    [_window addSubview:text1];
    text1.backgroundColor = [UIColor orangeColor];
    //给输入框赋初值
    text1.text = @"are you ready?";
    text1.textAlignment = 1;
    text1.textColor = [UIColor blueColor];
    text1.borderStyle = 3;//UITextBorderStyleNone默认无 UITextBorderStyleRoundedRect圆角 UITextBorderStyleLine边缘线 UITextBorderStyleBezel
    
    return YES;
}
- (void)handleButton:(UIButton *)button
{
    [_window endEditing:YES];
    NSLog(@"你已经拯救了世界!");
}
- (void)handleButton1:(UIButton *)button
{
    [_window endEditing:YES];
    NSLog(@"世界不需要弱者");
}
//开始编辑前 判断能否编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //yes or no
    return YES;
}
//已经开始编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}
//判断是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
